<template>
    <div class="admin">
        <el-card v-loading="pager.loading" class="mt-4 !border-none" shadow="never">
            <el-button type="primary" @click="handleAdd">
                <el-icon class="mr-1">
                    <Plus />
                </el-icon>
                新增
            </el-button>

            <div class="mt-4">
                <el-table :data="pager.lists" size="large" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" />
                    <el-table-column type="index" label="序号" width="60" align="center"></el-table-column>
                    <!-- <el-table-column label="ID" prop="id" min-width="60" />> -->

                    <el-table-column label="海报" min-width="100">
                        <template #default="{ row }">
                            <el-image :src="row.img" fit="contain" class="h-48" />
                        </template>
                    </el-table-column>
                    <el-table-column label="标题" prop="title" min-width="100" />

                    <el-table-column label="操作" fixed="right">
                        <template #default="{ row }">
                            <el-button type="primary" link @click="handleEdit(row)"> 编辑 </el-button>
                            <el-button type="info" link @click="handleDetail(row)"> 详情 </el-button>
                            <el-button v-if="row.root != 1" type="danger" link @click="handleDelete(row.id)">
                                删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="mt-4 flex justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>

<script lang="ts" setup name="admin">
import feedback from "@/utils/feedback";

import EditPopup from "./edit.vue";
import { usePaging } from "@/hooks/usePaging";
import Pagination from "@/components/pagination/index.vue";
import { deleteListOneAPI, getListAPI } from "@/api/list";

const editRef = shallowRef<InstanceType<typeof EditPopup>>();
// 查询表单数据
const formData = reactive({
    useId: "",
    bookId: "",
    content: ""
});

const selectedIds = ref<number[]>([]);

const showEdit = ref(false);
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: getListAPI,
    params: formData
});

const handleAdd = async () => {
    showEdit.value = true;
    await nextTick();
    editRef.value?.open("add");
};

const handleEdit = async (data: any) => {
    showEdit.value = true;
    await nextTick();
    editRef.value?.open("edit");
    editRef.value?.setFormData(data);
};

const handleDelete = async (id: string) => {
    await feedback.confirm("确定要删除？");
    await deleteListOneAPI(id);
    getLists();
};
const router = useRouter();
const handleDetail = (data: any) => {
    console.log("data", data);
    if (data.id !== "d31d") {
        feedback.notifyError("暂无详细信息");
        return;
    }
    router.push({ name: "detail", query: { id: data.id } });
};

const handleSelectionChange = (selection: any) => {
    selectedIds.value = selection.map((item: any) => item.id);
};

onMounted(() => {
    getLists();
});
</script>
